<?php
/**
 * 个人中心
 */

namespace app\api\controller;

use app\common\model\UserException;
use app\common\model\Users;
use chillerlan\QRCode\QRCode;
use think\facade\Cache;

class User extends Base
{

    //退出登录
    public function logout()
    {
        $user = $this->currentUser();
        $token = $this->essentialParam('token');
        Cache::store('redis')->delete("access_token-" . $user['id']);
        $user->token = "";
        if ($user->save()) {
            return $this->response('退出成功');
        } else {
            throw new UserException('退出失败');
        }
    }

    //更换手机号
    public function changeMobile()
    {
        $user = $this->currentUser();
        $mobile = $this->essentialParam('mobile');
        $check_code = $this->essentialParam('check_code');
        Users::changeMobile($user, $mobile, $check_code);
        return $this->response('更换成功');
    }

    //验证用户
    public function checkUser()
    {
        $user = $this->currentUser();
        $mobile = $this->essentialParam('mobile');
        $password = $this->essentialParam('password');
        $check_code = Users::checkUser($user, $mobile, $password);
        $this->insertResponse('check_code', $check_code);
        return $this->response('验证成功');
    }

    public function setPassword()
    {
        $user = $this->user_info();
        $pay_password = input('pass');
        $pay_password1 = input('again_pass');
        $res = $this->validate([
            "password" => $pay_password,
            "password2" => $pay_password1
        ], [
            "password|支付密码" => "require|length:6|number|confirm:password2"
        ]);
        if ($res !== true) {
            return $this->ajaxError($res);
        }
        $user->pay_password = md5($pay_password);
        $user->save();
        return $this->ajaxSuccess();


    }

    public function passwordStatus()
    {
        $user = $this->user_info();
        if (empty($user['pay_password'])) {
            $pay_password = 0;
        } else {
            $pay_password = 1;
        }
        return $this->ajaxSuccess(['pay_password' => $pay_password]);

    }

    //修改登录密码（原密码）
    public function setLoginPassword()
    {
        $user = $this->currentUser();
        $data = $this->requestData;
        unset($data['token']);
        $res = $this->validate($data, [
            "before_pass|原密码" => "require",
            "new_password|新密码" => "require|confirm:again_password",
        ]);
        if ($res !== true) {
            throw new UserException($res);
        }
        if ($user['password'] != md5($user['salt'] . $data['before_pass'] . $user['salt'])) {
            throw new UserException('原密码错误');
        }
        $passsword = md5($user['salt'] . $data['new_password'] . $user['salt']);
        $res = Users::where("id", $user['id'])->update([
            'password' => $passsword,
            'token' => ''
        ]);
        if (empty($res)) {
            throw new UserException('修改失败');
        } else {
            Cache::store('redis')->delete('access_token-' . $user['id']);
            return $this->response('修改成功');
        }
    }

    //注销账号
    public function destroyUser()
    {
        $user = $this->currentUser();
        $user->token = "";
        $user->username = "已注销";
        $user->mobile = "999999999";
        $res = $user->save();
        if (empty($res)) {
            throw new UserException('注销失败');
        } else {
            Cache::store('redis')->delete('access_token-' . $user['id']);
            return $this->response('注销成功');
        }

    }

    //修改个人资料
    public function modify()
    {
        $user = $this->currentUser();
        $avatar = $this->optionalParam('avatar');
        $gender = $this->optionalParam('gender');
        $birthday = $this->optionalParam('birthday');
        if ($avatar !== "") {
            $user->avatar = $avatar;
        }
        if ($gender !== "") {
            $user->gender = $gender;
        }
        if ($birthday !== "") {
            $user->birthday = $birthday;
        }
        if ($user->save()) {
            return $this->response('修改成功');
        } else {
            throw new UserException('修改失败');
        }
    }

    public function userInfo()
    {
        $user = $this->currentUser();
        $data = [
            "user_id" => $user['id'],
            "avatar" => $user['avatar'],
            "gender" => $user['gender'],
            "birthday" => $user['birthday'],
            "facebook_status" => $user['facebook_id'] ? 1 : 0,
            "google_status" => $user['google_id'] ? 1 : 0,
            "password_status" => $user['password'] ? 1 : 0,
            "tel" => show_star($user['mobile'], 3, 4)
        ];
        $this->insertResponse("data", $data);
        return $this->response("请求成功");


    }


    public function invite()
    {
        $user = $this->user_info();
        $invite_code = $user['invite_code'];
        $qr = new QRCode();
        $url = request()->domain(true) . '/h5/#/pages/login/index?states=1&invite_code=' . $invite_code;
        $poster = $qr->render($url);
        return $this->ajaxSuccess([
            'invite_code' => $invite_code,
            'share_tips' => '',
            'poster' => $poster
        ]);
    }

}



























